package com.liqi.web.service.dev;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liqi.common.utils.ReportUtil;
import com.liqi.dev.domain.CfgBranch;
import com.liqi.dev.domain.CfgService;
import com.liqi.dev.domain.CfgSubject;
import com.liqi.dev.mapper.CfgBranchMapper;
import com.liqi.dev.mapper.CfgServiceMapper;
import com.liqi.dev.mapper.CfgSubjectMapper;
import com.liqi.dev.pojo.ReportDTO;
import com.liqi.device.mapper.DvDeviceMapper;
import com.liqi.monitor.mapper.DvFaultLogMapper;
import com.liqi.statistics.mapper.DvServiceStatisticsMapper;

@Service
public class HomeService {

    @Autowired
    private CfgServiceMapper cfgServiceMapper;

    @Autowired
    private CfgSubjectMapper cfgSubjectMapper;

    @Autowired
    private CfgBranchMapper cfgBranchMapper;

    @Autowired
    private DvDeviceMapper dvDeviceMapper;

    @Autowired
    private DvServiceStatisticsMapper dvServiceStatisticsMapper;

    @Autowired
    private DvFaultLogMapper dvFaultLogMapper;

    public Integer serviceNum() {
        QueryWrapper<CfgService> qw = new QueryWrapper<>();
        qw.eq("enable", 1);
        qw.eq("status", 0);
        return cfgServiceMapper.selectCount(qw);
    }

    public Integer subjectNum() {
        QueryWrapper<CfgSubject> qw = new QueryWrapper<>();
        qw.eq("status", 0);
        return cfgSubjectMapper.selectCount(qw);
    }

    public Integer branchNum() {
        QueryWrapper<CfgBranch> qw = new QueryWrapper<>();
        qw.eq("status", 0);
        return cfgBranchMapper.selectCount(qw);
    }

    public Integer useDevNum() {
        return dvDeviceMapper.useDevNum();
    }

    public Integer devNum() {
        return dvDeviceMapper.devNum();
    }

    public Integer moduleNum() {
        return dvDeviceMapper.moduleNum();
    }

    public List<ReportDTO> top10() {
        return dvServiceStatisticsMapper.top10();
    }

    public List<ReportDTO> top10Percent() {
        List<ReportDTO> list = dvServiceStatisticsMapper.top10();
        int sum = list.stream().mapToInt(e -> e.getNum()).sum();
        Integer itemNum = dvServiceStatisticsMapper.itemNum();
        // 如果总数大于top10
        if (itemNum > sum) {
            list.add(new ReportDTO("其他", itemNum - sum));
        }
        return list;
    }

    public List<Map<String, Object>> near30faultLog() {
        List<String> near30Day = ReportUtil.near30Day();
        List<Map<String, Object>> list = new ArrayList<>(near30Day.size());
        for (String date : near30Day) {
            Map<String, Object> map = dvFaultLogMapper.faultLogDate(date);
            list.add(map);
        }
        return list;
        
    }

}
